package com.example.fence.util;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.ArrayList;
@Slf4j
public class FenceDetectUtil {
    /**
     * 多边形围栏区域判定:判定一个坐标点是否在一多边形区域(闭合多边形)内（电子围栏）
     */
    public static boolean PoiInPoly(List<String> pointList, String point) {
        //经纬度换算值
        double coord = 1;
        try {
            double x = MathExtend.divide(Double.parseDouble(point.split(",")[0]),
                    coord, 8);
            double y = MathExtend.divide(Double.parseDouble(point.split(",")[1]),
                    coord, 8);
            PoiInPolygon poiInPolygon = new PoiInPolygon();
            String[] xys = null;
            xys = new String[pointList.size()];
            pointList.toArray(xys);
            List<Point> poly = new ArrayList<Point>(xys.length);
            for (String xy : xys) {
                double px = MathExtend.divide(Double.parseDouble(xy.split(",")[0]),
                        coord, 8);
                double py = MathExtend.divide(Double.parseDouble(xy.split(",")[1]),
                        coord, 8);
                System.out.println(px);
                System.out.println(py);
                poly.add(new Point(px, py));
            }
            int pip = poiInPolygon.InPolygon(poly, new Point(x, y));
            return pip == 0 || pip == 1;
        }
        catch (NumberFormatException e){
            log.error("经纬度信息异常，无法检测");
            return false;
        }
    }
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("11.000000,40.000000");
        list.add("11.000000,80.000000");
        list.add("10.000000,80.000000");
        list.add("10.000000,40.000000");
        boolean b3 = PoiInPoly(list, "10.500000,80.000000");
        System.out.println(b3);
    }
}
